package huang.com.android.datepickerdialog;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private TextView showdate;
    private Button setdate;
    private int year;
    private int month;
    private int day;
    AlertDialog.Builder dialog = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        showdate=(TextView) this.findViewById(R.id.showtime);
        setdate = (Button) this.findViewById(R.id.setdate);
        //初始化Claendar日历对象
        Calendar c = Calendar.getInstance(Locale.CHINA);
        final Date mydate = new Date(); // 获取当前日期
        c.setTime(mydate); //为当前对象设置时间为当前日期

        year = c.get(Calendar.YEAR); //获取对象中的年
        month = c.get(Calendar.MONTH);//获取对象中的月
        day = c.get(Calendar.DAY_OF_MONTH);//获取这个月的第几天
        showdate.setText("当前日期:"+year+"-"+(month+1)+"-"+day); //显示当前的年月日

        //添加点击事件--设置日期
        setdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建DatePickerDialog对象
                DatePickerDialog my_datepickerDialog = new DatePickerDialog(MainActivity.this, DateListener,year,month,day);
                my_datepickerDialog.show(); //显示DatePickerDialog组件
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private DatePickerDialog.OnDateSetListener DateListener = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(DatePicker v, int y, int m, int d) {

            //修改y,m,d,的变量值，一遍以后单击按钮时，datapickerdialog上显示上一次修改后的值
            year = y;
            month = m;
            day = d;
            //更新日期
            updateDate();
        }
        //当datepickerdialog关闭时，更新日期显示

        private void updateDate() {
            Calendar c = Calendar.getInstance(Locale.CHINA);
            if(year < c.get(Calendar.YEAR)||month < c.get(Calendar.MONTH)|| day < c.get(Calendar.DAY_OF_MONTH)){
                //在textview上显示日期
                showdate.setText("当前日期:"+year+"-"+(month+1)+"-"+day);
            }else {
                dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("Warning");
                dialog.setMessage("你设置的日期超过当前日期，请重新设置");
                dialog.setCancelable(false);
                dialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "You clicked OK", Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.show();
            }
        }
    };

}






